//
//  UIColor+Add.swift
//  PublicPod
//
//  Created by 许俊华 on 2021/9/6.
//

import Foundation

import UIKit

public enum GradientType:Int {
    case GradientTypeTopToBottom = 0 // 从上到下
    case GradientTypeLeftToRight = 1 // 从左到右
    case GradientTypeUpleftToLowright //
    case GradientTypeUprightToLowleft
}

public extension UIColor{
    class func colorWithHexColor(hexadecimal:String)->UIColor{
        var cstr = hexadecimal.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines).uppercased() as NSString;
        if(cstr.length < 6){
            return UIColor.clear;
        }
        if(cstr.hasPrefix("0X")){
            cstr = cstr.substring(from: 2) as NSString
        }
        if(cstr.hasPrefix("#")){
            cstr = cstr.substring(from: 1) as NSString
        }
        if(cstr.length != 6){
            return UIColor.clear;
        }
        var range = NSRange.init()
        range.location = 0
        range.length = 2
        //r
        let rStr = cstr.substring(with: range);
        //g
        range.location = 2;
        let gStr = cstr.substring(with: range)
        //b
        range.location = 4;
        let bStr = cstr.substring(with: range)
        var r :UInt32 = 0x0;
        var g :UInt32 = 0x0;
        var b :UInt32 = 0x0;
        Scanner.init(string: rStr).scanHexInt32(&r);
        Scanner.init(string: gStr).scanHexInt32(&g);
        Scanner.init(string: bStr).scanHexInt32(&b);
        return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1);
    }
    
    /**
     渐变色
     */
    class func gradientColorImageFromColors(colors:[UIColor], gradientType:GradientType, imgSize:CGSize) -> UIColor {
        var ar:[CGColor] = [CGColor]()
        for c in colors {
            ar.append(c.cgColor)
        }
        
        UIGraphicsBeginImageContextWithOptions(imgSize, true, 1)
        
        let context:CGContext = UIGraphicsGetCurrentContext()!
        
        context.saveGState()
        
        let cccgColor:CGColor = colors[colors.endIndex-1].cgColor
        
        let colorSpace:CGColorSpace = cccgColor.colorSpace!
        
        let cfArray:CFArray = ar as CFArray
        let gradient:CGGradient = CGGradient(colorsSpace: colorSpace, colors: cfArray, locations: [0.0, 1.0])!
        
        var start:CGPoint
        var end:CGPoint
        
        switch gradientType {
            case .GradientTypeTopToBottom:
                start = CGPoint(x: 0.0, y: 0.0)
                end = CGPoint(x: 0.0, y: imgSize.height)
                break
            case .GradientTypeLeftToRight:
                start = CGPoint(x: 0.0, y: 0.0)
                end = CGPoint(x: imgSize.width, y: 0.0)
                break;
            case .GradientTypeUpleftToLowright:
                start = CGPoint(x: 0.0, y: 0.0)
                end = CGPoint(x: imgSize.width, y: imgSize.height)
                break;
            case .GradientTypeUprightToLowleft:
                start = CGPoint(x: imgSize.width, y: 0.0)
                end = CGPoint(x: 0.0, y: imgSize.height)
                break;
        }
        
        context.drawLinearGradient(gradient, start: start, end: end, options: CGGradientDrawingOptions(rawValue: CGGradientDrawingOptions.drawsBeforeStartLocation.rawValue | CGGradientDrawingOptions.drawsAfterEndLocation.rawValue))
        
        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        
        context.restoreGState()
        
        UIGraphicsEndImageContext()
        
        return UIColor.init(patternImage: image)
    }
}
